import { getUserInfo, login } from '@/api/user'
import { setToken, getToken } from '@/utils/auth'

const state = {
  token: getToken() || null,
  userInfo: {}
}
const mutations = {
  setToken(state, token) {
    state.token = token// 存仓库
    setToken(token)// 持久化存储
  },
  setUserInfo(state, userInfo) {
    state.userInfo = userInfo
  }
}
const actions = {
  async login({ commit }, obj) {
    const res = await login(obj)
    commit('setToken', res)
  },
  async  getUserInfo({ commit }) {
    const res = await getUserInfo()
    console.log(res)
    commit('setUserInfo', res)
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
